const get = (url, params, process) => {
	var token = "";
	let to = uni.getStorageSync("token") || [];
	if (to.length == 0) {
		uni.redirectTo({
			url: "../login/login"
		})
		return;
	}
	token = to;
	uni.request({
		url: "http://10.205.130.185:8888/" + url,
		method: 'GET',
		header: {
			'token': token,
			'content-type': 'application/x-www-form-urlencoded'
		},
		data: params,
		success: res => {
			res = res.data;
			if (res.code == "400006") {
				hideLoading();
				errorToast("token已过期");
				uni.removeStorageSync("token");
				uni.redirectTo({
					url: "../login/login"
				})
			} else if (res.code == 0) {
				hideLoading();
				process(res.data);
			} else if (res.code == 400008) {
				hideLoading();
				errorToast("该用户已被冻结");
				uni.removeStorageSync("token");
				uni.redirectTo({
					url: "../login/login"
				})
			} else {
				hideLoading();
				errorToast(res.errMsg);
			}

		},
		fail: () => {
			hideLoading();
			errorToast("网络连接失败");
			uni.redirectTo({
				url: "../login/login"
			});
		},
		complete: () => {
			
		}
	});
}
const post = (url, params, process) => {
	var token = "";
	let to = uni.getStorageSync("token") || [];
	if (to.length == 0) {
		uni.redirectTo({
			url: "../login/login"
		})
		return;
	}
	token = to;
	uni.request({
		url: "http://10.205.130.185:8888/" + url,
		method: 'POST',
		header: {
			'token': token,
			'content-type': 'application/x-www-form-urlencoded'
		},
		data: params,
		success: res => {
			res = res.data;
			if (res.code == "400006") {
				hideLoading();
				uni.removeStorageSync("token");
				uni.redirectTo({
					url: "/pages/login/login.vue"
				})
			} else if (res.code == 0) {
				hideLoading();
				process(res.data);
			} else if (res.code == 400008) {
				errorToast("该用户已被冻结");
				hideLoading();
				uni.removeStorageSync("token");
				uni.redirectTo({
					url: "../login/login"
				});
			} else {
				hideLoading();
				errorToast(res.errMsg);
			}

		},
		fail: () => {
			hideLoading();
			errorToast("网络连接失败");
			uni.redirectTo({
				url: "../login/login"
			});
		},
		complete: () => {
			
		}
	});
}
const formatTime = date => {
	date = new Date(date);
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const errorToast = msg => {
	// #ifdef APP-PLUS
	plus.nativeUI.toast(msg, {
		type: "text",
		duration: "short"
	})
	// #endif
	// #ifdef MP-WEIXIN
	uni.showToast({
		title: msg,
		mask: true,
		duration: 1500,
		icon: "none"
	});
	// #endif
}

const successToast = msg => {
	// #ifdef APP-PLUS
	plus.nativeUI.toast(msg, {
		type: "text",
		duration: "short"
	})
	// #endif
	// #ifdef MP-WEIXIN
	uni.showToast({
		title: msg,
		mask: true,
		duration: 1000,
		icon: "none"
	});
	// #endif
}
const loading = msg => {
	// #ifdef APP-PLUS
	plus.nativeUI.showWaiting(msg, {
		width: "100px",
		height: "100px",
		round: "10px",
		padding: "10px"
	});
	// #endif

	// #ifdef MP-WEIXIN
	uni.showLoading({
		title: msg,
		mask: true
	})
	// #endif
}

const hideLoading = () => {
	// #ifdef APP-PLUS
	plus.nativeUI.closeWaiting();
	// #endif

	// #ifdef MP-WEIXIN
	uni.hideLoading();
	// #endif

}

module.exports = {
	get,
	post,
	formatTime,
	errorToast,
	successToast,
	loading,
	hideLoading
}
